package com.dykj.fireFighting.web.utils;

import com.dykj.fireFighting.api.monitoringCenter.vo.PgRegionRecordScoreVo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IDEA
 *
 * @author:hxb
 * @Date: 2019/6/20 10:32
 * @Description:
 */
public class PgRegionRecordScoreVoTreeUtil {

    private List<PgRegionRecordScoreVo> nodes;

    /**
     * 创建一个新的实例 Tree.
     *
     * @param nodes
     */
    public PgRegionRecordScoreVoTreeUtil(List<PgRegionRecordScoreVo> nodes) {
        this.nodes = nodes;
    }

    /**
     * buildTree
     * 描述:  创建树
     *
     * @return
     */
    public List<PgRegionRecordScoreVo> buildTree() {
        List<PgRegionRecordScoreVo> list = new ArrayList<PgRegionRecordScoreVo>();
        for (PgRegionRecordScoreVo node : nodes) {
            //这里判断父节点，需要自己更改判断
            if (StringUtils.equals(node.getParentId(), "-1")) {
                PgRegionRecordScoreVo map = buildTreeChildsMap(node);
                list.add(map);
            }
        }
        return list;
    }

    /**
     * buildChildMap
     * 描述:生成Map节点
     *
     * @param childNode
     * @return
     */
    private PgRegionRecordScoreVo buildTreeChildsMap(PgRegionRecordScoreVo childNode) {
        PgRegionRecordScoreVo map = new PgRegionRecordScoreVo();

        map.setId(childNode.getId());
        map.setName(childNode.getName());
        map.setParentId(childNode.getParentId());
        map.setLevel(childNode.getLevel());
        map.setQz(childNode.getQz());
        map.setScore(childNode.getScore());
        map.setGxz(childNode.getGxz());
        map.setTotal(childNode.getTotal());
        List<PgRegionRecordScoreVo> childs = buildTreeChilds(childNode);
        if (!childs.isEmpty()) {
            map.setChildList(childs);
            //map.setTotal(childs.size());
        }
        return map;
    }

    /**
     * buildChilds
     * 描述:  创建树下的节点。
     *
     * @param node
     * @return
     */
    private List<PgRegionRecordScoreVo> buildTreeChilds(PgRegionRecordScoreVo node) {
        List<PgRegionRecordScoreVo> list = new ArrayList<PgRegionRecordScoreVo>();
        List<PgRegionRecordScoreVo> childNodes = getChilds(node);
        for (PgRegionRecordScoreVo childNode : childNodes) {
            PgRegionRecordScoreVo map = buildTreeChildsMap(childNode);
            list.add(map);
        }
        return list;
    }

    /**
     * getChilds
     * 描述:  获取子节点
     *
     * @param parentNode
     * @return List<Resource>
     */
    public List<PgRegionRecordScoreVo> getChilds(PgRegionRecordScoreVo parentNode) {
        List<PgRegionRecordScoreVo> childNodes = new ArrayList<PgRegionRecordScoreVo>();
        for (PgRegionRecordScoreVo node : nodes) {
            if (StringUtils.equals(node.getParentId(), parentNode.getId())) {
                childNodes.add(node);
            }
        }
        return childNodes;
    }

    /**
     * buildTree
     * 描述:  创建树
     *
     * @return List<Map < String, Object>>
     */
    public List<PgRegionRecordScoreVo> buildTreeGrid() {
        List<PgRegionRecordScoreVo> list = new ArrayList<PgRegionRecordScoreVo>();
        for (PgRegionRecordScoreVo node : nodes) {
            //这里判断父节点，需要自己更改判断
            if (StringUtils.equals(node.getParentId(), "-1")) {
                List<PgRegionRecordScoreVo> childs = buildTreeGridChilds(node);
                //node.setChildren(childs);
                list.add(node);
            }
        }
        return list;
    }

    /**
     * buildChilds
     * 描述:  创建树下的节点。
     *
     * @param node
     * @return List<Map < String, Object>>
     */
    private List<PgRegionRecordScoreVo> buildTreeGridChilds(PgRegionRecordScoreVo node) {
        List<PgRegionRecordScoreVo> list = new ArrayList<PgRegionRecordScoreVo>();
        List<PgRegionRecordScoreVo> childNodes = getChilds(node);
        for (PgRegionRecordScoreVo childNode : childNodes) {
            List<PgRegionRecordScoreVo> childs = buildTreeGridChilds(childNode);
            //childNode.setChildren(childs);
            list.add(childNode);
        }
        return list;
    }

}
